//
//  UIView+CLFrame.m
//
//  Created by Charlie on 15/4/21.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "UIView+CLFrame.h"

@implementation UIView (CLFrame)

- (CGPoint) origin {
    return self.frame.origin;
}

- (void) setOrigin:(CGPoint) point {
    CGRect frame = self.frame;
    frame.origin = point;
    self.frame = frame;
}

- (CGSize) size {
    return self.bounds.size;
}

- (void) setSize:(CGSize) size {
    self.bounds = CGRectMake(0, 0, size.width, size.height);
}

- (void)setFrameOffset:(CGPoint)origin size:(CGSize)size {
    CGRect frame = CGRectOffset(self.frame, origin.x, origin.y);
    frame.size = CGSizeMake(frame.size.width + size.width, frame.size.height + size.height);
    self.frame = frame;
}

- (CGFloat) x {
    return self.frame.origin.x;
}

- (void) setX:(CGFloat)x {
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat) y {
    return self.frame.origin.y;
}

- (void) setY:(CGFloat)y {
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

-(CGFloat)centerX{
    return self.center.x;
}

-(void)setCenterX:(CGFloat)centerX{
    CGPoint center=self.center;
    center.x=centerX;
    self.center =center;
}

-(CGFloat)centerY{
    return self.center.y;
}


-(void)setCenterY:(CGFloat)centerY{
    CGPoint center =self.center;
    center.y=centerY;
    self.center =center;
}


- (CGFloat) height {
    return self.frame.size.height;
}
- (void)setHeight:(CGFloat)height {
    CGRect frame = self.bounds;
    frame.size.height = height;
    self.bounds = frame;
}

- (CGFloat)width {
    return self.frame.size.width;
}
- (void)setWidth:(CGFloat)width {
    CGRect frame = self.bounds;
    frame.size.width = width;
    self.bounds = frame;
}

- (CGFloat)bottom {
    return CGRectGetMaxY(self.frame);
}

- (CGFloat)right {
    return CGRectGetMaxX(self.frame);
}

- (CGRect)convertFrameToSuperview:(UIView *)superview {
    
    CGRect rect = self.frame;
    UIView *v = self.superview;
    while (v != superview && v) {
        rect.origin.x += v.frame.origin.x;
        rect.origin.y += v.frame.origin.y;
        v = v.superview;
    }
    if (v == superview) {
        return rect;
    } else {
        return CGRectNull;
    }
}

@end
